package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.utils.MenuHelper;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> findNodes() {
        // 查询所有菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectAll();
        if (CollectionUtils.isEmpty(sysMenuList)) return null;
        // 调用工具类的方法 把返回的list封装要求数据格式
        List<SysMenu> treeList = MenuHelper.bulidTree(sysMenuList);
        return treeList;
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);

        // 新添加子菜单的时候，把父菜单isHalf半开状态1
        updateSysRoleMenu(sysMenu);
    }

    private void updateSysRoleMenu(SysMenu sysMenu) {
        // 查询是否存在父节点
       SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        // 将id设置为半开
       if (parentMenu != null) {
           sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());
       }

       updateSysRoleMenu(parentMenu);



    }

    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    @Override
    public void removeById(Long id) {
        int count = sysMenuMapper.countByParentId(id);
        if (count > 0) {
            throw  new GuiguException(ResultCodeEnum.NODE_ERROR);
        }

        sysMenuMapper.deleteById(id);
    }


    // 查询用户可以操作菜单
    @Override
    public List<SysMenuVo> findMenusByUserId() {
        // 获取用户id
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();

        // 根据userId查询可以操作的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.findMenusByUserId(userId);

        // 封装要求数据格式 返回
        List<SysMenu> sysMenuList1 = MenuHelper.bulidTree(sysMenuList);
        List<SysMenuVo> sysMenuVos = this.bulidMenus(sysMenuList1);

        return sysMenuVos;


    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> bulidMenus(List<SysMenu> menus) {
        List<SysMenuVo> sysMenuVoList = new LinkedList<>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(bulidMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);

        }

        return sysMenuVoList;

    }



}
